package com.cda.utils;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * @ClassName: PictureMarkProcessor
 * @Auther: zhangyingqi
 * @Date: 2018/9/11 09:20
 * @Description: 添加图片水印
 */
public class PictureMarkProcessor {

    /**
     * @param args
     */
    public static void main(String[] args) {

        //new PictureMarkProcessor().testPictureMark();
        //new PictureMarkProcessor().testPictureMark(new File("C:/mysoftware/images/upload/report/91440400303839794J/test.png"),new File("C:/mysoftware/images/upload/report/water6.png"));
    }

    public void testPictureMark(File srcImageFile, File logoImageFile, File outputRoateImageFile) {

        //srcImageFile = new File("C:/mysoftware/images/upload/report/91440400303839794J/test.png");
        //logoImageFile = new File("C:/mysoftware/images/upload/report/water3.png");

        //File outputRoateImageFile = new File("C:/mysoftware/images/upload/report/desktop_pic_mark.jpg");

        createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, -45);
    }

    public void createWaterMarkByIcon(File srcImageFile, File logoImageFile, File outputImageFile, Integer degree) {

        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(srcImageFile);

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            Graphics2D graphics = buffImg.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
                    srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

            ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
            Image logoImg = logoImgIcon.getImage();

            //旋转
            if (null != degree) {
                graphics.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getWidth() / 2);
            }

            float alpha = 0.5f; // 透明度
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            //水印图片的位置
            int width = srcImg.getWidth (null);// 原图宽度
            int height = srcImg.getHeight (null);// 原图高度
            int x = -width/ 2;
            int y = -height/ 2;
            int markWidth = logoImgIcon.getIconWidth();// 水印长度
            int markHeight = logoImgIcon.getIconHeight();// 水印高度
            // 循环添加水印
            while (x < width * 1.5) {
                y = -height / 2;
                while (y < height * 1.5) {
                    graphics.drawImage (logoImg, x, y,null);

                    y += markHeight + 600;
                }
                x += markWidth + 600;
            }
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            graphics.dispose();

            os = new FileOutputStream(outputImageFile);
            // 生成图片
            ImageIO.write(buffImg, "JPG", os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




    public void createWaterMarkByIconByIo(BufferedImage srcImg, File logoImageFile, File outputImageFile, Integer degree) {

        OutputStream os = null;
        try {
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            Graphics2D graphics = buffImg.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
                    srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

            ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
            Image logoImg = logoImgIcon.getImage();

            //旋转
            if (null != degree) {
                graphics.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getWidth() / 2);
            }

            float alpha = 0.5f; // 透明度
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            //水印图片的位置
            int width = srcImg.getWidth (null);// 原图宽度
            int height = srcImg.getHeight (null);// 原图高度
            int x = -width/ 2;
            int y = -height/ 2;
            int markWidth = logoImgIcon.getIconWidth();// 水印长度
            int markHeight = logoImgIcon.getIconHeight();// 水印高度
            // 循环添加水印
            while (x < width * 1.5) {
                y = -height / 2;
                while (y < height * 1.5) {
                    graphics.drawImage (logoImg, x, y,null);

                    y += markHeight + 600;
                }
                x += markWidth + 600;
            }
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            graphics.dispose();

            os = new FileOutputStream(outputImageFile);
            // 生成图片
            ImageIO.write(buffImg, "JPG", os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




}